// page/detail/pages/returnOrder/returnOrder.js/
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    domain: app.api.domain,
    centerData: [],
    type: 3,
    p: 0,
    isEnd: false,
    isEmpty: true,
    navData: [{
      name: '全部',
      type: '3',
      index: 0
    },
    {
      name: '处理中',
      type: '1',
      index: 1
    },
    {
      name: '已完成',
      type: '2',
      index: 2
    }
    ],
    page: 1,
    itemIndex: 0
  },

  // 初始化分页、为空标识和上拉结束标识
  init() {
    const that = this;
    that.setData({
      p: 0,
      isEnd: true,
      isEmpty: true,
      centerData: [],
      isEnd: false
    })
  },

  // 切换选项卡
  switchTab(e) {
    const that = this;
    console.log(e)
    that.init();
    that.setData({
      itemIndex: e.currentTarget.dataset.index,
      type: e.currentTarget.dataset.type,
      p: 1,
      isEnd: false
    })
    console.log(that.data.itemIndex)
    that.getData()
  },
  // 进入售后详情
  toServerSheet(e) {
    console.log(e)
    wx.navigateTo({
      url: `/page/detail/pages/serverSheet/serverSheet?id=${e.currentTarget.dataset.order_sn}`,
    })
  },
  getData() {
    const that = this;
    that.setData({
      hiddenLoading: true
    })
    let json = {
      token: app.globalData.token,
      type: that.data.type,
      p: 1
    }
    that.setData({
      hiddenLoading: false
    })
    console.log(json)
    let jsonData = app.utils.getSignData(json);
    app.utils.post(app.api.reqUrls.afterSale, jsonData).then(res => {
      that.setData({
        hiddenLoading: true
      })
      if (res.status == 1) {
        if (res.result == null) {
          that.setData({
            isEmpty: false
          })
          return false
        } else {
          res.result.forEach(item => {
            // 格式化时间戳
            item.ctime = app.moment(item.add_time * 1000).format('YYYY-MM-DD');
            //判断处理状态
            switch (item.after_status) {
              case 1:
                item.orders_status = '待审核';
                return false;
              case 2:
                item.orders_status = '已完成';
                return false;
              case 5:
                item.orders_status = '用户取消';
                return false;
            }
          })
          that.setData({
            isEmpty: true,
            centerData: res.result
          })
        }

      } else {
        throw res.msg
      }
    }).catch(err => {
      that.setData({
        hiddenLoading: true
      })
      app.showInfo(err.toString())
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const that = this;
    that.getData()
    //数据统计
    app.statistics("page/detail/pages/evaluate/evaluate", "退换货")
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    const that = this;
    let page = that.data.page;
    let center = that.data.centerData;
    page++;
    let json = {
      token: app.globalData.token,
      type: that.data.type,
      p: page
    }
    that.setData({
      hiddenLoading: false
    })
    console.log(json)
    let jsonData = app.utils.getSignData(json);
    app.utils.post(app.api.reqUrls.afterSale, jsonData).then(res => {
      that.setData({
        hiddenLoading: true
      })
      if (res.status == 1) {
        if (res.result == null) {
          that.setData({
            isEnd: true
          })
          return false
        } else {

          res.result.forEach(item => {
            // 格式化时间戳
            item.ctime = app.moment(item.add_time * 1000).format('YYYY-MM-DD');
            // 判断处理状态
            switch (item.after_status) {
              case 1:
                item.orders_status = '待审核';
                return false;
              case 2:
                item.orders_status = '已完成';
                return false;
              case 5:
                item.orders_status = '用户取消';
                return false;
            }

          })
          res.result.forEach(item => {
            center.push(item)

          })
          that.setData({
            isEmpty: true,
            centerData: center,
            page
          })
        }

      } else {
        throw res.msg
      }
    }).catch(err => {
      that.setData({
        hiddenLoading: true
      })
      app.showInfo(err.toString())
    })

  }

})